$(function() {

	var result = Model.user.selectUser();
	model(result);

})

// 删除
$(document).on('click', '.delete', function() {
	var userid = $(this).attr("userid");
	var confirms = weui.confirm("真的要删除信息吗？", function() {
		var result = Model.user.deleteUser({
			userid : userid
		})
		if (result.code == 1) {
			weui.toast('删除成功', {
				duration : 1500,
				className : 'custom-classname',
				callback : function() {
					location.reload()
				}
			});

		}

	}, function() {
	});

})

// 修改
$(document).on('click', '.update-btn', function() {
	$(".shade").css("display", "block");
	var userid = $(this).attr("userid");
	var resultUpdate = Model.user.findUserById({
		userid : userid
	})
	$(".userid").val(userid);
	$(".name").val(resultUpdate.u.name);
	$(".sex").val(resultUpdate.u.sex);
	$(".address").val(resultUpdate.u.address);
	$(".postcode").val(resultUpdate.u.postcode);
	$(".confirm-btn").click(function() {

		var name = $(".name").val();
		var resultCheckName = Model.user.findUserByName({
			name : name
		})
		if (resultCheckName.code == 1) {
			weui.alert("用户名不可用")
			return false;
		}
		var sex = $(".sex").val();
		var address = $(".address").val();
		var postcode = $(".postcode").val();
		var info = {
			name : $.trim($('.name').val()),
			phone : $.trim($('.sex').val()),
			address : $.trim($('.address').val()),
			code : $.trim($('.postcode').val()),
		};
		for (i in info) {
			if (info[i] == '') {
				weui.alert('信息不得为空');
				return false;
			}
		}

		$("#updateForm").submit();
		weui.toast('修改成功', {
			duration : 1500,
			className : 'custom-classname',
			callback : function() {
				location.reload()
			}
		});

	})

})

// 分页插件
function model(result) {
	var gridColumns_2_2_2 = [

			{
				id : 'userid',
				title : '客户ID',
				type : 'string',
				columnClass : 'text-center',
				fastQuery : true,
				fastQueryType : 'eq'
			},
			{
				id : 'name',
				title : '客户名称',
				type : 'string',
				columnClass : 'text-center',
				fastQuery : true,
				fastQueryType : 'eq'
			},
			{
				id : 'sex',
				title : '地址',
				type : 'string',
				columnClass : 'text-center',
				fastQuery : true,
				fastQueryType : 'eq'
			},
			{
				id : 'address',
				title : '邮箱',
				type : 'string',
				columnClass : 'text-center',
				fastQuery : true,
				fastQueryType : 'eq'
			},
			{
				id : 'postcode',
				title : '邮编',
				type : 'string',
				columnClass : 'text-center',
				fastQuery : true,
				fastQueryType : 'eq'
			},
			{
				id : 'userid',
				title : '操作',
				type : 'string',
				columnClass : 'text-center',
				fastQuery : true,
				fastQueryType : 'eq',
				resolution : function(value, record, column, grid, dataNo,
						columnNo) {
					var htmlTmpl = '<div><a href="javascript:;" class="btn btn-success update-btn" userid="'
							+ record['userid']
							+ '">修改</a><a href="javascript:;" class="btn btn-warning delete" userid="'
							+ record['userid'] + '">删除</a></div>';
					return htmlTmpl;
				}

			} ];
	var gridOption_2_2_2 = {
		lang : 'zh-cn',
		ajaxLoad : false,
		loadAll : true,
		datas : result.user,
		columns : gridColumns_2_2_2,
		gridContainer : 'gridContainer_2_2_2',
		toolbarContainer : 'gridToolBarContainer_2_2_2',
		tools : '',
		pageSize : 5,
		pageSizeLimit : [ 1, 10, 20, 50 ]
	};
	var grid_2_2_2 = $.fn.dlshouwen.grid.init(gridOption_2_2_2);
	grid_2_2_2.load();
}
